package com.benz.project.pvm.service;

import com.benz.project.pvm.domain.Person;
import com.benz.project.pvm.domain.Project;
import com.benz.project.pvm.domain.Role;
import com.benz.project.pvm.repository.ProjectRepository;
import com.benz.project.pvm.repository.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectService {

    @Autowired
    private ProjectRepository projectRepository;

    /**
     * 新增项目
     * @param name
     * @param desc
     * @param team
     * @param startTime
     * @param endTime
     * @param status
     * @return projectId, 返回项目的id
     */
    public String addProject(String name,
                           String desc,
                           String team,
                           Date startTime,
                           Date endTime,
                           Integer status){
        Project project = new Project();
        project.setId(UUID.randomUUID().toString());
        project.setName(name);
        project.setDesc(desc);
        project.setTeam(team);
        project.setStartTime(startTime);
        project.setEndTime(endTime);
        project.setStatus(status);
        projectRepository.save(project);
        return project.getId();
    }

    /**
     * 修改项目
     * @param id
     * @param name
     * @param desc
     * @param team
     * @param startTime
     * @param endTime
     * @param status
     */
    public void updateProject(String id,
                              String name,
                              String desc,
                              String team,
                              Date startTime,
                              Date endTime,
                              Integer status){
//        Date startTimeDate = Date.parse(startTime);
        Project project = projectRepository.getOne(id);
        project.setName(name);
        project.setDesc(desc);
        project.setTeam(team);
        project.setStartTime(startTime);
        project.setEndTime(endTime);
        project.setStatus(status);
        projectRepository.save(project);
    }

    /**
     * 删除项目
     * @param ids
     */
    public void deleteProjects(String[] ids){
        for(String id : ids){
            Project project = projectRepository.getOne( id );
//            List<Role> roles = project.getRoles();
//            project.getRoles().removeAll( roles );
//            List<Person> persons = project.getPersons();
//            project.getPersons().removeAll( persons );
            projectRepository.deleteById(id);
        }
    }

    /**
     * 获取单个项目信息
     * @param id
     * @return
     */
    public Project getProject(String id){
        return projectRepository.getOne(id);
    }

    /**
     * 获取所有项目信息
     * @return
     */
    public List<Project> getProjects(){
        return projectRepository.findAll();
    }

}
